using System;
using System.Collections;


// 定义抽象处理器
public abstract class Handler
{
    protected Handler _next;

    public virtual void SetNext(Handler next)
    {
        _next = next;
    }

    public virtual Handler GetNext()
    {
        return _next;
    }

    public abstract void HandleRequest(string request);
}

// 定义具体处理器1
public class ConcreteHandler1 : Handler
{
    public override void HandleRequest(string request)
    {
        if (request == "one")
        {
            Console.WriteLine("ConcreteHandler1 handles request one.");
        }
        else if (_next != null)
        {
            _next.HandleRequest(request);
        }
    }
}

// 定义具体处理器2
public class ConcreteHandler2 : Handler
{
    public override void HandleRequest(string request)
    {
        if (request == "two")
        {
            Console.WriteLine("ConcreteHandler2 handles request two.");
        }
        else if (_next != null)
        {
            _next.HandleRequest(request);
        }
    }
}

// 客户端代码
public class ChainOfResponsibilityPattern
{
    public static void Main(string[] args)
    {
        Handler handler1 = new ConcreteHandler1();
        Handler handler2 = new ConcreteHandler2();
        handler1.SetNext(handler2);

        handler1.HandleRequest("one"); // 输出: ConcreteHandler1 handles request one.
        handler1.HandleRequest("two"); // 输出: ConcreteHandler2 handles request two.
        handler1.HandleRequest("three"); // 不会被处理，因为没有更多的处理器
    }
}

//输出结果：
//ConcreteHandler1 handles request one.
//ConcreteHandler2 handles request two.